﻿using System;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Intel_Client_GUI
{
	public class ProcessViewModel : INotifyPropertyChanged
	{
		public ProcessViewModel()
		{
			// 在此点下面插入创建对象所需的代码。
			db = Globle.DbWrap;
			Globle.DateTimeData.OnTimeChanged+=new Intel_Client_GUI.DateTimeModel.DateTimeChangeHandler(DateTimeData_OnTimeChanged);
		}
		~ProcessViewModel()
		{
			Globle.DateTimeData.OnTimeChanged-=this.DateTimeData_OnTimeChanged;
		}
		public DataTable ChartSource
		{
			get
			{
				if(dt==null)
					dt = this.CalcData(Globle.DateTimeData.TimeAndDuration);
				return dt;
			}
			set{
				if(dt!=value)
				{
					dt = value;
					this.NotifyPropertyChanged("ChartSource");
				}
			}
		}
		public DataTable SubChartMouseSource
		{
			get
			{
				return subMouseDt;
			}
			set
			{
				subMouseDt = value;
				this.NotifyPropertyChanged("SubChartMouseSource");
			}
		}
		public DataTable SubChartKeySource
		{
			get
			{
				return subKeyDt;
			}
			set
			{
				subKeyDt = value;
				this.NotifyPropertyChanged("SubChartKeySource");
			}
		}
		
		
		private DataTable CalcData(DateTimeModel.TimeInfo Info)
		{
			var b  = db.GetQueryTable(@" select sum(YValue) as cnt,AxisXLabel as Label
								from(
									select count(1) as YValue,process as AxisXLabel from MouseRecords where timetick  group by process
    								union all
     								select count(1) as YValue,process as AxisXLabel from KeyRecords where timetick  group by process 
								) temp
								group by Label order by cnt desc limit 8",Info.start,Info.duration);
			if(b!=null)
				return b;
			return null;
		}
		
		private DataTable dt =null;
		
		private DataTable subMouseDt = null;
		
		private DataTable subKeyDt = null;
		
		private DataBaseWrap db;
		
		#region INotifyPropertyChanged
		public event PropertyChangedEventHandler PropertyChanged;

		
		private void NotifyPropertyChanged(String info)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(info));
			}
		}
		#endregion
		private void DateTimeData_OnTimeChanged(Intel_Client_GUI.DateTimeModel.TimeInfo info)
		{
			// 在此处添加事件处理程序实现。
			this.ChartSource = this.CalcData(info);
		}
		public void ShowSubChart(object ProcessName)
		{
			string MouseSQL = string.Format(@"select count(1) as cnt,strftime('%Y-%m-%d',timestamp)  as Date from MouseRecords where Process='{0}' and timetick group by Date order by Date",ProcessName);
			string KeySQL = string.Format(@"select count(1) as cnt,strftime('%Y-%m-%d',timestamp)  as Date from KeyRecords where Process='{0}' and timetick group by Date order by Date",ProcessName);
			//System.Windows.MessageBox.Show(MouseSQL);
			var Mdt = db.GetQueryTable(MouseSQL,Globle.DateTimeData.TimeAndDuration.start,Globle.DateTimeData.TimeAndDuration.duration);
			var Kdt = db.GetQueryTable(KeySQL,Globle.DateTimeData.TimeAndDuration.start,Globle.DateTimeData.TimeAndDuration.duration);
			this.SubChartKeySource = Kdt;
			this.SubChartMouseSource = Mdt;
		}
	}
}